package com.wandousoushu.jiusen

import android.app.Application
import android.os.Bundle
import android.view.KeyEvent
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemReselectedListener
import com.wandousoushu.jiusen.help.BookHelp
import com.wandousoushu.jiusen.ui.bookmail.BookMailFragment
import com.wandousoushu.jiusen.ui.bookshelf.BookShelfFragment
import com.wandousoushu.jiusen.ui.my.MyFragment
import org.jetbrains.anko.toast
import java.util.*

class MainActivity : FragmentActivity(), BottomNavigationView.OnNavigationItemSelectedListener, OnNavigationItemReselectedListener, OnPageChangeListener {
    private val mFragmentList: MutableList<Fragment> = ArrayList()
    private var mViewPager: ViewPager? = null
    var bottomNavigationView: BottomNavigationView? = null
    private var pagePosition = 0
    private var exitTime: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bottomNavigationView = findViewById(R.id.nav_view)
        bottomNavigationView!!.setItemIconTintList(null)
        mViewPager = findViewById(R.id.view_pager_main)
        val f1: Fragment = BookMailFragment()
        val f2: Fragment = BookShelfFragment()
        val f3: Fragment = MyFragment()
        mFragmentList.add(f1)
        mFragmentList.add(f2)
        mFragmentList.add(f3)
        //mViewPager.setOffscreenPageLimit(3);
        val fm = supportFragmentManager
        val adapter = TabFragmentPageAdapter(fm)
        mViewPager!!.setAdapter(adapter)
        mViewPager!!.setCurrentItem(intent.getIntExtra("position",0))
        mViewPager!!.addOnPageChangeListener(this)
        bottomNavigationView!!.setOnNavigationItemSelectedListener(this)
        bottomNavigationView!!.setOnNavigationItemReselectedListener(this)
    }

    override fun onNavigationItemReselected(item: MenuItem) {}
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.navigation_bookmail -> {
                mViewPager!!.currentItem = 0
                pagePosition = 0
            }
            R.id.navigation_bookshelf -> {
                mViewPager!!.currentItem = 1
                pagePosition = 1
            }
            R.id.navigation_my -> {
                mViewPager!!.currentItem = 2
                pagePosition = 2
            }
        }
        return false
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
    override fun onPageSelected(position: Int) {
        bottomNavigationView!!.menu.getItem(position).isChecked = true
    }

    override fun onPageScrollStateChanged(state: Int) {}
    private inner class TabFragmentPageAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        override fun getItem(position: Int): Fragment {
            return mFragmentList[position]
        }

        override fun getCount(): Int {
            return mFragmentList.size
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        MyApplication.instance.Clear()
        BookHelp.clearRemovedCache()
    }

    fun getFragment(position: Int):Fragment{
        return mFragmentList[position]
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        event?.let {
            when (keyCode) {
                KeyEvent.KEYCODE_BACK ->  {
                    if (pagePosition != 0) {
                        mViewPager!!.currentItem = 0
                        pagePosition=0
                        return true
                    }
                    if (System.currentTimeMillis() - exitTime > 2000) {
                        toast(R.string.double_click_exit)
                        exitTime = System.currentTimeMillis()
                    } else {
                        finish();
                        System.exit(0)
                    }
                    return true
                }

//                KeyEvent.KEYCODE_BACK -> if (event.isTracking && !event.isCanceled) {
//                    if (pagePosition != 0) {
//                        mViewPager!!.currentItem = 0
//                        return true
//                    }
//                    if (System.currentTimeMillis() - exitTime > 2000) {
//                        toast(R.string.double_click_exit)
//                        exitTime = System.currentTimeMillis()
//                    } else {
//                        finish()
//                    }
//                    return true
//                }
                else -> {
                }
            }
        }
        return super.onKeyUp(keyCode, event)
    }
}